/********************************
 * Robinson Matrix Algorithm
 *
 * Copyright (C) 2009 CNRS
 * Author : Florent AVELLANEDA, Eugen CEPOI
 * 
 *
 * All rights reserved.
 *
 *   This file is part of Robinson Matrix Algorithm.
 *
 *   Robinson Matrix Algorithm is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robinson Matrix Algorithm is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Robinson Matrix Algorithm.  If not, see <http://www.gnu.org/licenses/>.
 *
 ***********************************************************************************/
#include "Clause.h"

#include <iostream>

#include "Var.h"

namespace SAT
{

    Clause::Clause ( )
    {

    }

    Clause::~Clause ()
    {

    }

    void Clause::inverce( std::set< std::set< std::pair<Var*, bool> > >::iterator begin, std::set< std::set< std::pair<Var*, bool> > >::iterator end, std::set< std::pair<Var*, bool > > result )
    {
        if( begin == end )
        {
            _ssData.insert( result );
            return;
        }

        std::set< std::set< std::pair<Var*, bool> > >::iterator next = begin;
        ++next;

        std::pair<Var*, bool > e;
        for (std::set<std::pair<Var*, bool> >::iterator it = begin->begin(); it != begin->end(); ++it)
        {
            e = *it;
            e.second = !e.second;
            result.insert( e );
            inverce( next, end, result );
            result.erase( e );
        }
    }


    Clause Clause::operator! () const
    {
        Clause result;

        result.inverce( _ssData.begin(), _ssData.end(), std::set< std::pair<Var*, bool > >() );        


        return result;
    }

    Clause Clause::operator* (const Clause &c) const
    {
        Clause result = *this;

        for (std::set< std::set< std::pair< Var*, bool > > >::iterator it = c._ssData.begin(); it != c._ssData.end(); ++it)
            result._ssData.insert( *it );

        return result;
    }

    Clause Clause::operator* (Var &v) const
    {
        Clause result = *this;

        std::set< std::pair<Var*, bool> > s;

        s.insert( std::pair< Var*, bool>(&v, true) );

        result._ssData.insert( s );

        return result;
    }

    Clause Clause::operator+ (const Clause &c) const
    {
        Clause result;


        for (std::set< std::set< std::pair< Var*, bool > > >::iterator it = _ssData.begin(); it != _ssData.end(); ++it)
        {
            for (std::set< std::set< std::pair< Var*, bool > > >::iterator it2 = c._ssData.begin(); it2 != c._ssData.end(); ++it2)
            {
                std::set< std::pair< Var*, bool > > s( *it );

                for (std::set< std::pair< Var*, bool > >::iterator it3 = it2->begin(); it3 != it2->end(); ++it3)
                    s.insert( *it3 );

                result._ssData.insert( s );
            }
        }

        return result;
    }

    Clause Clause::operator+ (Var &v) const
    {
        Clause result;

        for (std::set< std::set< std::pair< Var*, bool > > >::iterator it = _ssData.begin(); it != _ssData.end(); ++it)
        {
            std::set< std::pair< Var*, bool > > s( *it );

            s.insert( std::pair<Var*, bool>( &v, true ) );

            result._ssData.insert( s );
        }

        return result;
    }



    void Clause::afficher()
    {
        std::cerr << "val = " << std::endl;
        for (std::set< std::set< std::pair< Var*, bool > > >::iterator it = _ssData.begin(); it != _ssData.end(); ++it)
        {
            for (std::set< std::pair< Var*, bool > >::iterator it2 = it->begin(); it2 != it->end(); ++it2)
            {
                if(!it2->second)
                    std::cerr << "!";
                std::cerr << *(it2->first) << " + ";
            }
            std::cerr << std::endl;
        }
    }

    Clause operator! (Var &v)
    {
        Clause result;
        result = result * v;
        return !result;
    }

    Clause operator+ (Var &v1, Var &v2)
    {
        Clause result;

        result = (result * v1) + v2;

        return result;
    }

    Clause operator* (Var &v1, Var &v2)
    {
        Clause result;

        result = result * v1 * v2;

        return result;
    }

    Clause operator+ (Var &v, const Clause &c)
    {
        return c+v;
    }

    Clause operator* (Var &v, const Clause &c)
    {
        return c*v;
    }

}
